<?php

/**
 * @author gbfun <gbfun@163.com>  2017-09-16 20:03
 * http://www.wuweiyou.com
 */

class db_table_good extends wwy_db_table
{
    protected $_name = 'good_good';
    
    protected static $_instance;
    
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    public function find($id, $status = null)
    {
        $read_db = $this->_get_read_db();
                
        $sql = '
            SELECT id, name, brief, description, after_sale_service, click, image, 
                `order`, status, `if_show_number_grade`, create_time, update_time, cat_id, 
                min_price, max_price  
            FROM ' . $this->_full_name . ' 
            WHERE id = ?                 
        ';
        $bind = array($id);
        
        if($status !== null){
            $sql .= ' AND status = ?';
            $bind[] = $status;    
        }
               
        $row = $read_db->fetch_row($sql, $bind);
        
        return $row;
    }
    
    public function fetch_all($where = null, $order = null, $count = null, $offset = null)
    {
        $read_db = $this->_get_read_db();
        
        $where      = $this->get_where($where);
        $where_str  = $where['where_str'];
        $where_bind = $where['where_bind'];
        
        $order_str = '';
        if($order !== null){
            if(!is_array($order)){
                $order = array($order);
            }
            $order_str = 'ORDER BY ' . implode(', ', $order) . ' ';
        }
        
        $sql = '
            SELECT id, name, brief, description, after_sale_service, click, image, 
                `order`, status, create_time, update_time, cat_id, 
                min_price, max_price 
            FROM ' . $this->_full_name . ' 
            ' . $where_str . '
            ' . $order_str . '
        ';
        
        if($count !== null){
            $sql = $read_db->limit($sql, $count, $offset);
        }
        
        $rows = $read_db->fetch_all($sql, $where_bind);
        
        return $rows;
    }
    
    public function get_where($where)
    {
        if(isset($where['where_str'])){
            return $where;
        }
    
        $read_db = $this->_get_read_db();
    
        $where_str = '';
        $where_bind = array();
        if(isset($where['ids'])){
            $ids = $read_db->quote($where['ids']);
            $where_str .= 'id IN (' . $ids . ') ';
        }
        if(isset($where['cat_ids'])){
            $cat_ids = $read_db->quote($where['cat_ids']);
            $where_str .= 'cat_id IN (' . $cat_ids . ') ';
        }
        if(isset($where['status'])){
            if($where_str != ''){
                $where_str .= 'AND ';
            }
            $status_quote = $read_db->quote($where['status']);
            $where_str .= 'status IN (' . $status_quote . ') ';
        }
        
        if(isset($where['id'])){
            if($where_str != ''){
                $where_str .= 'AND ';
            }
            $where_str    .= 'id = ? ';
            $where_bind[]  = $where['id'];
        }
        if(isset($where['name'])){
            if($where_str != ''){
                $where_str .= 'AND ';
            }
            $where_str    .= 'name LIKE ? ';
            $where_bind[]  = '%' . $where['name'] . '%';
        }
    
        if($where_str != ''){
            $where_str = 'WHERE ' . $where_str;
        }
    
        $where = array('where_str' => $where_str, 'where_bind' => $where_bind);
    
        return $where;
    }
    
    public function get_list($where = null, $order = null, $options = array())
    {
        $where     = $this->get_where($where);
        $count     = $this->get_count($where);
        $page_data = $this->get_page_data($count, $options);
    
        $limit  = $page_data['page_item_count'];
        $offset = $page_data['start_offset'];
        $list = $this->fetch_list($where, $order, $limit, $offset);
    
        $list  = array('list' => $list, 'page_data' => $page_data);
         
        return $list;
    }
    
    public function get_count($where)
    {
        $read_db = $this->_get_read_db();
    
        $where      = $this->get_where($where);
        $where_str  = $where['where_str'];
        $where_bind = $where['where_bind'];
    
        $sql = '
            SELECT COUNT(`id`) AS count
            FROM ' . $this->_full_name . '
            ' . $where_str . '
        ';
        $row = $read_db->fetch_row($sql, $where_bind);
    
        if(!empty($row['count'])){
            $count = intval($row['count']);
        }else{
            $count = 0;
        }
    
        return $count;
    }
    
    public function fetch_list($where = null, $order = null, $count = null, $offset = null)
    {
        $list = $this->fetch_all($where, $order, $count, $offset);
    
        return $list;
    }
    
    public function get_attr_list($where = null, $order = null)
    {
        $read_db = $this->_get_read_db();
    
        $where_str = '';
        $where_bind = array();
        
        if(isset($where['good_id'])){
            $where_str    .= 'good_id = ? ';
            $where_bind[]  = $where['good_id'];
        }
    
        if($where_str != ''){
            $where_str = 'WHERE ' . $where_str;
        }
    
        $order_str = '';
        if($order !== null){
            if(!is_array($order)){
                $order = array($order);
            }
            $order_str = 'ORDER BY ' . implode(', ', $order) . ' ';
        }
    
        $sql = '
            SELECT id, good_id, name, value, `order` 
            FROM `good_good_attr` 
            ' . $where_str . '
            ' . $order_str . '
        ';
    
        $rows = $read_db->fetch_all($sql, $where_bind);
    
        return $rows;
    }
    
    public function get_sattr_list($where = null, $order = null)
    {
        $read_db = $this->_get_read_db();
    
        $where_str = '';
        $where_bind = array();
    
        if(isset($where['good_id'])){
            $where_str    .= 'good_id = ? ';
            $where_bind[]  = $where['good_id'];
        }
    
        if($where_str != ''){
            $where_str = 'WHERE ' . $where_str;
        }
    
        $order_str = '';
        if($order !== null){
            if(!is_array($order)){
                $order = array($order);
            }
            $order_str = 'ORDER BY ' . implode(', ', $order) . ' ';
        }
    
        $sql = '
            SELECT id, good_id, name, `order`
            FROM `good_good_sattr` 
            ' . $where_str . '
            ' . $order_str . '
        ';
    
        $rows = $read_db->fetch_all($sql, $where_bind);
    
        return $rows;
    }
    
    public function get_number_grade_list($where = null, $order = null)
    {
        $read_db = $this->_get_read_db();
    
        $where_str = '';
        $where_bind = array();
    
        if(isset($where['good_id'])){
            $where_str    .= 'good_id = ? ';
            $where_bind[]  = $where['good_id'];
        }
    
        if($where_str != ''){
            $where_str = 'WHERE ' . $where_str;
        }
    
        $order_str = '';
        if($order !== null){
            if(!is_array($order)){
                $order = array($order);
            }
            $order_str = 'ORDER BY ' . implode(', ', $order) . ' ';
        }
    
        $sql = '
            SELECT id, good_id, `number_min`, `number_max`, `order`
            FROM `good_good_number_grade`
            ' . $where_str . '
            ' . $order_str . '
        ';
    
        $rows = $read_db->fetch_all($sql, $where_bind);
    
        return $rows;
    }
    
    public function get_spec_list($where = null, $order = null)
    {
        $read_db = $this->_get_read_db();
    
        $where_str = '';
        $where_bind = array();
    
        if(isset($where['good_id'])){
            $where_str    .= 'good_id = ? ';
            $where_bind[]  = $where['good_id'];
        }
    
        if($where_str != ''){
            $where_str = 'WHERE ' . $where_str;
        }
    
        $order_str = '';
        if($order !== null){
            if(!is_array($order)){
                $order = array($order);
            }
            $order_str = 'ORDER BY ' . implode(', ', $order) . ' ';
        }
    
        $sql = '
            SELECT id, good_id, `order` 
            FROM `good_good_spec` 
            ' . $where_str . '
            ' . $order_str . '
        ';
        //var_dump($sql); exit();
    
        $rows = $read_db->fetch_all($sql, $where_bind);
    
        return $rows;
    }
    
    public function get_spec_item($good_id, $spec_id)
    {
        $read_db = $this->_get_read_db();

        $where_bind = array($good_id, $spec_id);
    
        $sql = '
            SELECT id, good_id, `order`  
            FROM `good_good_spec`
            WHERE good_id = ? AND id = ?;
        ';
    
        $row = $read_db->fetch_row($sql, $where_bind);
        //var_dump($row); exit();
    
        return $row;
    }
    
    public function get_spec_sattr_value_list($where = null)
    {
        $read_db = $this->_get_read_db();
    
        $where_str = '';
        $where_bind = array();
    
        if(isset($where['good_spec_id'])){
            $where_str    .= 'good_spec_id = ? ';
            $where_bind[]  = $where['good_spec_id'];
        }
    
        if($where_str != ''){
            $where_str = 'WHERE ' . $where_str;
        }
    
        $sql = '
            SELECT id, good_spec_id, good_sattr_id, value 
            FROM `good_good_spec_sattr`
            ' . $where_str . '
        ';
    
        $rows = $read_db->fetch_all($sql, $where_bind);
    
        return $rows;
    }
    
    public function get_spec_number_grade_price_list($where = null)
    {
        $read_db = $this->_get_read_db();
    
        $where_str = '';
        $where_bind = array();
    
        if(isset($where['good_spec_id'])){
            $where_str    .= 'good_spec_id = ? ';
            $where_bind[]  = $where['good_spec_id'];
        }
    
        if($where_str != ''){
            $where_str = 'WHERE ' . $where_str;
        }
    
        $sql = '
            SELECT id, good_spec_id, good_number_grade_id, price 
            FROM `good_good_spec_number_grade`
            ' . $where_str . '
        ';
    
        $rows = $read_db->fetch_all($sql, $where_bind);
    
        return $rows;
    }
    
    public function get_match_spec_number_grade_price($where = null)
    {
        $read_db = $this->_get_read_db();
    
        $where_str = '';
        $where_bind = array();
    
        if(isset($where['good_spec_id'])){
            $where_str    .= 'good_spec_id = ? ';
            $where_bind[]  = $where['good_spec_id'];
        }
        
        if(isset($where['good_number_grade_id'])){
            if($where_str != ''){
                $where_str .= 'AND ';
            }            
            $where_str    .= 'good_number_grade_id = ? ';
            $where_bind[]  = $where['good_number_grade_id'];
        }
    
        if($where_str != ''){
            $where_str = 'WHERE ' . $where_str;
        }
    
        $sql = '
            SELECT id, good_spec_id, good_number_grade_id, price
            FROM `good_good_spec_number_grade`
            ' . $where_str . '
        ';
           
        $row = $read_db->fetch_row($sql, $where_bind);
        
        return $row;
    }
    
    public function increase_click($id)
    {
        $write_db = $this->_get_write_db();
    
        $sql = '
            UPDATE ' . $this->_full_name . '
            SET
                `click` = `click` + 1
    
            WHERE `id` = ?
        ';
    
        $bind = array(    
            $id,
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
    
        return $result;
    }
}

